import Vuex from 'vuex'
import Vue from 'vue'
import {
  locToken,
  locUserInfo,
  locBaseData,
  locBaseSet,
  locTabbarData,
  locTimeAreaInfo,
  locAreaInfo,
  cinemaInfo,
  locSharePosterInfo,
  locHasShowModel,
  locChoosetfilmName,
  locGlobalDialog,
  locAccount,
  locPassword
} from '@/utils/locStore/data'
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    userInfo: locUserInfo.get(), // 用户信息,
    baseData: locBaseData.get(), // 基础配置信息,
    token: locToken.get(),
    tabbarData: locTabbarData.get(), // 底部菜单栏信息
    baseSet: locBaseSet.get(), // 系统信息
    areaInfo: locTimeAreaInfo.get(), // 选中的定位信息
    locAreaInfo: locAreaInfo.get(), // 真实定位信息
    cinemaInfo: cinemaInfo.get(), //选中影院信息
    share: locSharePosterInfo.get() || {}, // 分享信息
    hasShowModel: locHasShowModel.get(),
    chooseFilmName: locChoosetfilmName.get(), // 选择座位影院名称
    globalDialog: locGlobalDialog.get() // 全局弹框
  },
  actions: {
    setUserInfo({ commit }, info) {
      commit('SET_USER_INFO', info)
    },
    setBaseData({ commit }, info) {
      commit('SET_BASE_DATA', info)
    },
    setToken({ commit }, info) {
      commit('SET_TOKEN', info)
    },
    setTabbarData({ commit }, info) {
      commit('SET_TABBAR_DATA', info)
    },

    setBaseSet({ commit }, info) {
      commit('SET_BASE_SET', info)
    },
    setChooseAreaInfo({ commit }, info) {
      commit('SET_AREA_INFO', info)
    },

    setLocAreaInfo({ commit }, info) {
      commit('SET_LOC_AREA_INFO', info)
    },
    setCinemaInfo({ commit }, info) {
      console.log('setCinemaInfo', info)
      commit('SET_CINEMA_INFO', info)
    },
    clearAll({ commit }, info) {
      commit('CLEAR_ALL', info)
    },
    setPosterInfo({ commit }, info) {
      commit('SET_POSTER_INFO', info)
    },
    setHasShowModel({ commit }, info) {
      commit('SET_HAS_SHOW_MODEL', info)
    },
    setChooseFilmName({ commit }, info) {
      commit('SET_CHOOSE_FILM_NAME', info)
    },
    setGlobalDialog({ commit }, info) {
      commit('SET_GLOBAL_DIALOG', info)
    },
    // 获取token
    getToken({ commit }) {
      console.log('getToken1')
      return new Promise(async (resolve, reject) => {
        try {
          console.log('getToken2')
          const res = await uni.$api.public.getToken(
            {},
            { checkToken: false, queue: 'sync' }
          )
          console.log('getToken', res)
          commit('SET_TOKEN', res.tokenId)
          resolve(res)
        } catch (error) {
          console.log('getToken error', error)
          reject(error)
        }
      })
    },
    // 获取getBaseSet
    getBaseSet({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.channel.baseSet(
            {},
            { checkToken: false, queue: 'sync' }
          )

          commit('SET_BASE_SET', res)
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    },
    // 获取account
    getAccount({ commit }) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.public.getAccount(
            {},
            { checkToken: false, queue: 'sync' }
          )
          locAccount.set(res.account)
          locPassword.set(res.password)
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    },
    async initApp({ dispatch }) {
      //#ifndef H5
      await dispatch('getAccount')
      // #endif
      await dispatch('getBaseSet')
      await dispatch('getToken')
    },
    // 错误日志
    errorLog(_, data = {}) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await uni.$api.public.addSystemErrorLog(data, {
            handleError: false
          })
          resolve(res)
        } catch (error) {
          reject(error)
        }
      })
    }
  },
  mutations: {
    SET_USER_INFO(state, info) {
      const res = {
        ...info,
        memberLevel: +info.memberLevel === 3 ? 0 : info.memberLevel
      }
      locUserInfo.set(res)
      state.userInfo = res
    },
    SET_BASE_DATA(state, info) {
      locBaseData.set(info)
      state.baseData = info
    },
    SET_TOKEN(state, token) {
      locToken.set(token)
      state.token = token
    },
    SET_TABBAR_DATA(state, info) {
      locTabbarData.set(info)
      state.tabbarData = info
    },
    SET_BASE_SET(state, info) {
      locBaseSet.set(info)
      state.baseSet = info
    },
    SET_AREA_INFO(state, info) {
      locTimeAreaInfo.set(info)
      state.areaInfo = info
    },

    SET_LOC_AREA_INFO(state, info) {
      locAreaInfo.set(info)
      state.locAreaInfo = info
    },
    SET_CINEMA_INFO(state, info) {
      cinemaInfo.set(info)
      state.cinemaInfo = info
    },
    SET_POSTER_INFO(state, info) {
      console.log('state', state, info)
      const params = {
        ...info,
        showUser: +info.showUser
      }
      state.share = params
      locSharePosterInfo.set(params)
    },
    SET_HAS_SHOW_MODEL(state, info) {
      locHasShowModel.set(info)
      state.hasShowModel = info
    },
    SET_CHOOSE_FILM_NAME(state, info) {
      locChoosetfilmName.set(info)
      state.chooseFilmName = info
    },
    SET_GLOBAL_DIALOG(state, info) {
      locGlobalDialog.set(info)
      state.globalDialog = info
    },

    CLEAR_ALL() {
      locUserInfo.remove()
      locBaseData.remove()
      locBaseSet.remove()
      locTabbarData.remove()
      locTimeAreaInfo.remove()
      locAreaInfo.remove()
      cinemaInfo.remove()
      locHasShowModel.remove()
      locChoosetfilmName.remove()
      locSharePosterInfo.remove()
      locGlobalDialog.remove()
    }
  },
  getters: {
    userInfo: (state) => state.userInfo,
    memberLevel: (state) => state.userInfo && state.userInfo.memberLevel,
    baseData: (state) => state.baseData,
    memberId: (state) => state?.userInfo?.memberId || '',
    token: (state) => state.token,
    tabbarData: (state) => state.tabbarData,
    baseSet: (state) => state.baseSet,
    areaInfo: (state) => state.areaInfo,
    locAreaInfo: (state) => state.locAreaInfo,
    cinemaInfo: (state) => state.cinemaInfo,
    cinemaName: (state) => state.cinemaInfo?.cinemaName,
    cityCinema: (state) => {
      return {
        cityId: state.areaInfo?.cityId,
        cinemaId: state.cinemaInfo?.cinemaId
      }
    },
    cinemaId: (state) => state.cinemaInfo?.cinemaId,
    shareInfo: (state) => state.share,
    showUser: (state) => !!state.share.showUser,
    hasShowModel: (state) => state.hasShowModel,
    chooseFilmName: (state) => state.chooseFilmName,
    globalDialog: (state) => state.globalDialog
  }
})

export default store
